/**
 * 数组中的flat方法
 * 将多维数组转为低位数组
 */

const arr = [1, 2, 3, [4, 5, [6, 7, 8]]];
// console.log(arr.flat());

// console.log(Object.fromEntries(Object.entries(arr)));

function myFlat(arr, depth = 1) {
  return arr.reduce((pre, cur) => {
    if (depth <= 1) {
      return pre.concat(cur);
    }
    return pre.concat(Array.isArray(cur) ? myFlat(cur, depth - 1) : cur);
  }, []);
}

console.log(myFlat(arr, 3));
